package org.cvcsc.weproject.application;

import org.cvcsc.weproject.controller.dto.LoginRequest;
import org.cvcsc.weproject.controller.dto.LoginResponse;
import org.cvcsc.weproject.domain.Student;
import org.cvcsc.weproject.service.AuthenticationService;
import org.cvcsc.weproject.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginApplicationService {

    @Autowired
    private AuthenticationService authenticationService;
    @Autowired
    private JwtUtil jwtUtil;

    public LoginResponse login(LoginRequest request) {
        Student student = authenticationService.authenticate(request.getNumber(), request.getPassword());
        String token = jwtUtil.generateToken(student); // 生成Token
        return new LoginResponse(
                student.getId(),
                student.getStudentNameAndNumber().getName(),
                student.isAdmin(),
                student.getClazz() != null ? student.getClazz().getId() : null,
                token);
    }
}